// setupBewrite.js
function bewriteHtmlWithData(data){
	var chapterid = data.id;
	var bewrite = data.bewrite;
	var is_dis_bewrite = data.is_dis_bewrite;
	var content_type = data.content_type;
	var isTag = '0';
	if (content_type == 'tag') {//如果是标签
		isTag = '1';
	}
	if (is_dis_bewrite == '0') {//不显示描述
		return '';
	}else if ($.trim(deleteGif(bewrite)) == ''){//去掉gif并去掉左右空格
		return '';
	}
	var bewriteHtml = richtextHtmlWithStr(bewrite);
	return '<div chapterid="'+chapterid+'" isTag="'+isTag+'" class="bewrite-cell weui_cell">'+
			'<div id="bewrite_div'+chapterid+'" class="bewrite-div">'+
				'<div class="bewrite-content">'+
					bewriteHtml+
				'</div>'+
			'</div>'+
		'</div>';
	// return '<div chapterid="'+chapterid+'" isTag="'+isTag+'" class="bewrite-cell weui_cell">'+
	// 		'<div id="bewrite_div'+chapterid+'" class="bewrite-div bewrite-div-min">'+
	// 			'<div class="bewrite-content">'+
	// 				bewriteHtml+
	// 			'</div>'+
	// 			'<div id="bewrite_hide'+chapterid+'" chapterid="'+chapterid+'" class="bewrite-bottom-hide" hidden="hidden">'+
	// 				'<span class="bewrite-bottom-span">'+
	// 					'收起'+
	// 					'<img class="icon-bewrite" src="../../image/icon_bewrite_up.png">'+
	// 				'</span>'+
	// 			'</div>'+
	// 			'<div id="bewrite_show'+chapterid+'" chapterid="'+chapterid+'" class="bewrite-bottom-show">'+
	// 				'<span class="bewrite-bottom-span">'+
	// 					'展开更多'+
	// 					'<img class="icon-bewrite" src="../../image/icon_bewrite_down.png">'+
	// 				'</span>'+
	// 			'</div>'+
	// 		'</div>'+
	// 	'</div>';
}
function showBewriteClick(){
	$('.bewrite-bottom-show').unbind('click');
	$('.bewrite-bottom-show').click(function(event){
		var chapterid = $(this).attr('chapterid');
		$('#bewrite_div'+chapterid).removeClass('bewrite-div-min');
		$('#bewrite_div'+chapterid).addClass('bewrite-div-max');
		$('#bewrite_hide'+chapterid).show();
		$(this).hide();
		event.stopPropagation();
	});
}
function hideBewriteClick(){
	$('.bewrite-bottom-hide').unbind('click');
	$('.bewrite-bottom-hide').click(function(event){
		var chapterid = $(this).attr('chapterid');
		$('#bewrite_div'+chapterid).removeClass('bewrite-div-max');
		$('#bewrite_div'+chapterid).addClass('bewrite-div-min');
		$('#bewrite_show'+chapterid).show();
		$(this).hide();
		event.stopPropagation();
	});
}
function deleteGif(string){
	var gifE = /<img(?:[^>]*)src=[\"|\']((?:[^>]*)\.gif)[\"|\'](?:[^>]*)>/gi;
    string = string.replace(gifE, "");
    return string;
}
function richtextHtmlWithStr(string){
	var other = string.replace(/\+/g, " ");
	var newWidth = api.winWidth - 40;
	other = other.replace(/width:420;/g, "width='" + newWidth + "'");
	other = other.replace(/width:640;/g, "width='" + newWidth + "'");
	other = other.replace(/width="518"/g, "width='" + newWidth + "'");
	// var gifE = /<img(?:[^>]*)src=[\"|\']((?:[^>]*)\.gif)[\"|\'](?:[^>]*)>/gi;
    // other = other.replace(gifE, "");
    // other = other.replace(/<p>/g, "");
    // other = other.replace(/<\/p>/g, "");
    other = other.replace(/style=\"list-style-type: decimal;\"/g, "style=\"list-style-type: decimal; margin-left: 14px;\"");
    other = other.replace(/src=\"\/public/g, "src=\"http://cfnet.org.cn/public");
    other = other.replace(/border-color: windowtext;/g, "");
    other = other.replace(/border-left-color: windowtext;/g, "");
    other = other.replace(/border-top-color: windowtext;/g, "");
    other = other.replace(/border-right-color: windowtext;/g, "");
    other = other.replace(/border-bottom-color: windowtext;/g, "");
    other = other.replace(/href="/g,'href="javascript:void(0);');
	return other;
}
function setupBewriteClick() {
	showBewriteClick();
	hideBewriteClick();
	tapImagClick();
	setupHtmlImage();
}
function tapImagClick() {
	$('.bewrite-content img').unbind('click');
	$('.bewrite-content img').on('click', function(event) {
		// openImageController([$(this).attr("src")], 0);
		var imgurl = $(this).attr("src");
		var arr = new Array('jpg', 'png', 'jpeg');
		var gifArr = new Array('gif', 'bmp');
		var ext = imgurl.replace(/.+\./, "");
		if (gifArr.toString().indexOf(ext) > -1){
			openGif(imgurl);
		}else if(arr.toString().indexOf(ext) > -1) {
			openImage(imgurl);
		}
		event.stopPropagation();
	});
}
//gif预览
function openGif(filepath){
	api.openWin({useWKWebView:true,
	    name : 'ShowImageFrame',
		url : '../Study/ShowImageFrame.html',
		bgColor : '#F0F0F0',
	    pageParam: {
	        filepath : filepath,
	    },
	    vScrollBarEnabled : true,
		hScrollBarEnabled : false,
		slidBackEnabled : false
	});
}
function openImage(imageurl){
	api.openWin({useWKWebView:true,
	    name: 'PicturePreviewPage',
	    url: '../Study/PicturePreviewPage.html',
	    pageParam: {
	        imageurl: imageurl
	    },
		slidBackEnabled : false
	});
}
//图片预览
function openImageController(imgages, i) {
	imgages = arraryWithObjc(imgages);
	var imageBrowser = api.require('imageBrowser');
	imageBrowser.openImages({
		imageUrls : imgages,
		showList : false,
		activeIndex : i,
	}, function(ret, err) {
	});
}
function arraryWithObjc(str) {
	if (str.constructor == Object) {
		return [];
	} else if (str.constructor == Array) {
		return str;
	} else if (str.constructor == String) {
		return str.split(",");
	}
	return [];
}
function setupHtmlImage() {
	// var newWidth = api.winWidth - 50;
	$(".bewrite-content img").each(function(i) {
		// var oldWith = $(this).width();
		// if (oldWith == 0) {
		// 	setTimeout(function() {
		// 		setupHtmlImage();
		// 	}, 500);
		// 	return;
		// }
		// if (oldWith > newWidth) {
		// 	var oldHeight = $(this).height();
		// 	var newHeight = oldHeight * (newWidth / oldWith);
		// 	$(this).width(newWidth);
		// 	$(this).height(newHeight);
		// }
		// $(this).width(newWidth);
		$(this).attr('style', 'max-width:100%;');
		$(this).height('auto');
	});
}